package com.lxl.bt.view;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.text.TextUtils;
import android.util.Log;
import android.view.Display;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.databinding.DataBindingUtil;

import com.bigkoo.pickerview.listener.OnOptionsSelectListener;
import com.lxl.bt.Constant;
import com.lxl.bt.R;
import com.lxl.bt.activity.ShiMinRenZhenActivity;
import com.lxl.bt.bean.AlertCallback;
import com.lxl.bt.bean.CreateOrderBody;
import com.lxl.bt.bean.DianliBean;
import com.lxl.bt.bean.ProductBean;
import com.lxl.bt.databinding.PopwindowBuyBinding;
import com.lxl.bt.net.NetApi;
import com.lxl.bt.net.Result;
import com.lxl.bt.retorfit.NetCallback;
import com.lxl.bt.retorfit.RetrofitUtil;
import com.lxl.bt.util.PickViewUtil;
import com.lxl.bt.util.Prefs;
import com.yzq.zxinglibrary.encode.CodeCreator;


/**
 * 自定义对话框
 */
public class CustomCommonDialog extends Dialog {

    public CustomCommonDialog(Context context) {
        super(context);
    }

    public CustomCommonDialog(Context context, int theme) {
        super(context, theme);
    }

    public static void closeDialog(Dialog mWeiboDialog) {
        if (mWeiboDialog != null && mWeiboDialog.isShowing()) {
            mWeiboDialog.dismiss();
            mWeiboDialog = null;
        }
    }


    public static class Builder {
        public static Context context;
        private static String title;
        private static String message;
        private static int messageTextColor;
        private static String LeftText;
        private static int LeftTextColor;
        private static String RightText;
        private static int RightTextColor;
        private static Boolean LeftTextShow;
        private static Boolean RightTextShow;
        private static Boolean Cancelable;
        private static Boolean CanceledOnTouchOutside;

        private static String positiveButtonText;
        private static String negativeButtonText;
        private String positiveButtonTextColor;
        private String negativeButtonTextColor;
        private OnCancelListener cancelListener;
        private OnClickListener positiveButtonClickListener;
        private OnClickListener negativeButtonClickListener;

        public String getTitle() {
            return title;
        }

        public static String getMessage() {
            return message;
        }

        public static int getMessageTextColor() {
            return messageTextColor;
        }

        public Builder setMessageTextColor(int messageTextColo) {
            messageTextColor = messageTextColo;
            return this;
        }

        public static String getLeftText() {
            return LeftText;
        }

        public Builder setLeftText(String leftText) {
            LeftText = leftText;
            return this;
        }

        public static int getLeftTextColor() {
            return LeftTextColor;
        }

        public Builder setLeftTextColor(int leftTextColor) {
            LeftTextColor = leftTextColor;
            return this;
        }

        public static String getRightText() {
            return RightText;
        }

        public Builder setRightText(String rightText) {
            RightText = rightText;
            return this;
        }

        public static int getRightTextColor() {
            return RightTextColor;
        }

        public Builder setRightTextColor(int rightTextColor) {
            RightTextColor = rightTextColor;
            return this;
        }

        public static Boolean getLeftTextShow() {
            return LeftTextShow;
        }

        public Builder setLeftTextShow(Boolean leftTextShow) {
            LeftTextShow = leftTextShow;
            return this;
        }

        public static Boolean getRightTextShow() {
            return RightTextShow;
        }

        public Builder setRightTextShow(Boolean rightTextShow) {
            RightTextShow = rightTextShow;
            return this;
        }

        public static String getPositiveButtonText() {
            return positiveButtonText;
        }

        public Builder setPositiveButtonText(String positiveButtonText) {
            this.positiveButtonText = positiveButtonText;
            return this;
        }

        public static String getNegativeButtonText() {
            return negativeButtonText;
        }

        public Builder setNegativeButtonText(String negativeButtonText) {
            this.negativeButtonText = negativeButtonText;
            return this;
        }

        public String getPositiveButtonTextColor() {
            return positiveButtonTextColor;
        }

        public String getNegativeButtonTextColor() {
            return negativeButtonTextColor;
        }

        public OnCancelListener getCancelListener() {
            return cancelListener;
        }

        public OnClickListener getPositiveButtonClickListener() {
            return positiveButtonClickListener;
        }

        public void setPositiveButtonClickListener(OnClickListener positiveButtonClickListener) {
            this.positiveButtonClickListener = positiveButtonClickListener;
        }

        public OnClickListener getNegativeButtonClickListener() {
            return negativeButtonClickListener;
        }

        public Builder setNegativeButtonClickListener(OnClickListener negativeButtonClickListener) {
            this.negativeButtonClickListener = negativeButtonClickListener;
            return this;
        }

        public static Boolean getCanceledOnTouchOutside() {
            return CanceledOnTouchOutside;
        }

        public static Boolean getCancelable() {
            return Cancelable;
        }

        public static int getGravity() {
            return gravity;
        }


        public static int gravity;
        public static boolean fullScreen;

        //        public Builder(Context context, boolean fullScreen) {
//            this.context = context;
//            this.fullScreen = fullScreen;
//        }
//        public Builder(Context context, boolean fullScreen,boolean cancelable) {
//            this.context = context;
//            this.fullScreen = fullScreen;
//            this.Cancelable=cancelable;
//        }
        public Builder(Context context, boolean fullScreen, boolean cancelable, boolean canceledOnTouchOutside, int gravity) {
            this.context = context;
            this.fullScreen = fullScreen;
            this.Cancelable = cancelable;
            this.CanceledOnTouchOutside = canceledOnTouchOutside;
            this.gravity = gravity;
        }

//        public Builder(Context context, int gravity) {
//            this.context = context;
//            this.gravity = gravity;
//        }

        public Builder setMessage(String message) {
            this.message = message;
            return this;
        }

        /**
         * Set the Dialog message from resource
         *
         * @param
         * @return
         */
        public Builder setMessage(int message) {
            this.message = (String) context.getText(message);
            return this;
        }

        /**
         * Set the Dialog title from resource
         *
         * @param title
         * @return
         */
        public Builder setTitle(int title) {
            this.title = (String) context.getText(title);
            return this;
        }

        /**
         * 设定 dialog 外围是否可点击
         */
        public void setCanceledOnTouchOutside(Boolean canceledOnTouchOutside) {
            CanceledOnTouchOutside = canceledOnTouchOutside;
        }

        /**
         * 设定 dialog 外围是否可点击
         */
        public void setCancelable(Boolean Cancelable) {
            Cancelable = Cancelable;
        }

        /**
         * Set the Dialog title from String
         *
         * @param title
         * @return
         */

        public Builder setTitle(String title) {
            this.title = title;
            return this;
        }

        public Builder setContentView(View v) {
            return this;
        }


        /**
         * 取消监听
         *
         * @param listener
         */

        public Builder setCancelListener(OnCancelListener listener) {
            this.cancelListener = listener;
            return this;
        }
        // 3

        public void setPositiveButton(String positiveButtonText, OnClickListener listener,
                                      boolean visiblePositive) {
            setPositiveButton(positiveButtonText, listener, true);
        }

        public void setNegativeButton(String negativeButtonText, OnClickListener listener,
                                      boolean visibleNegative) {
            setNegativeButton(negativeButtonText, listener, visibleNegative);
        }


        public Builder setPositiveButton(String positiveButtonText, OnClickListener listener) {
            this.positiveButtonText = positiveButtonText;
            this.positiveButtonClickListener = listener;
            return this;
        }


        public Builder setNegativeButton(String negativeButtonText, OnClickListener listener) {
            this.negativeButtonText = negativeButtonText;
            this.negativeButtonClickListener = listener;
            return this;
        }

        public Builder setPositiveButtonTextColor(String positiveButtonTextColor) {
            this.positiveButtonTextColor = positiveButtonTextColor;
            return this;
        }

        public Builder setNegativeButtonTextColor(String negativeButtonTextColor) {
            this.negativeButtonTextColor = negativeButtonTextColor;
            return this;
        }


        public CustomCommonDialog setAnimatorSet(CustomCommonDialog commonDialog, int styleId) {
            commonDialog.getWindow().setWindowAnimations(styleId);
            return commonDialog;
        }


        /**
         * 提示
         **/
        public static CustomCommonDialog hintDialog() {
            LayoutInflater inflater = (LayoutInflater) context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            final CustomCommonDialog dialog = new CustomCommonDialog(context, R.style.dialog);
            View layout = inflater.inflate(R.layout.popwindow_hint, null);
            ImageView iv = layout.findViewById(R.id.iv);
            Bitmap bitmap = CodeCreator.createQRCode(TextUtils.isEmpty(Prefs.getStringValue(Constant.CARDNO)) ? Prefs.getStringValue(Constant.PHONE) : Prefs.getStringValue(Constant.CARDNO), 400, 400, null);
            iv.setImageBitmap(bitmap);
            dialog.setCancelable(getCancelable());


            dialog.setContentView(layout);
            dialog.setCanceledOnTouchOutside(getCanceledOnTouchOutside());
            Window dialogWindow = dialog.getWindow();
            dialogWindow.setGravity(getGravity());
            return dialog;
        }


        public static CustomCommonDialog buyDialog(ProductBean product, DianliBean dianli, AlertCallback alertCallback) {
            String[] pays = {"支付宝", "银联", "btc", "usdt"};
            String[] payValues = {"1", "2", "3", "4"};
            int[] icons = {R.mipmap.zhifubao, R.mipmap.yinlian, R.mipmap.btc, R.mipmap.ustd};
            final int[] initPosition = {0};

            LayoutInflater inflater = (LayoutInflater) context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            final CustomCommonDialog dialog = new CustomCommonDialog(context, R.style.dialog);
            View layout = inflater.inflate(R.layout.popwindow_buy, null);
            PopwindowBuyBinding bind = DataBindingUtil.bind(layout);
            if (null != dianli) {
                bind.tvTotalPrice.setText(String.format("￥ %s", dianli.getAmount()));
                bind.tvOrderPrice.setText(String.format("￥ %s", dianli.getAmount()));
            }
            bind.tvShangjin.setText("0");
            bind.llPayType.setOnClickListener(v ->
                    PickViewUtil.createPickView(v.getContext(), (options1, options2, options3, v1) -> {
                        bind.ivPay.setImageResource(icons[options1]);
                        initPosition[0] = options1;
                    }, "支付方式", pays, initPosition[0])
            );
//            amountView.setGoods_storage(Integer.MAX_VALUE);
//            amountView.setOnAmountChangeListener(new AmountView.OnAmountChangeListener() {
//                @Override
//                public void onAmountChange(View view, long amount) {
//                    if (null != dianli) {
//                        tvTotalPrice.setText(String.format("￥ %s", dianli.getAmount()));
//                        tvOrderPrice.setText(String.format("￥ %s", dianli.getAmount()));
//                    }
//
//                }
//            });
//            amountView.setGoods_storage(goodBean.getNum());

            bind.ivDel.setOnClickListener(view -> dialog.dismiss());
            bind.tvCommit.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {

                    ////优惠券id
                    //private Long couponId;
                    //
                    //// 产品id
                    //private Long productId;
                    //
                    ////产品价格
                    //private BigDecimal payPrice;
                    //
                    ////支付类型value = "1:支付宝2：银联3：usdt4:btc"
                    //private String payType;
                    //
                    ////购买数量
                    //private String num;
                    RetrofitUtil.getInstance().create(NetApi.class, 0)
                            .createOrder("", dianli.getDictDetailId(), "", "", "1", product.getProductId()).enqueue(new NetCallback<Result<CreateOrderBody>>() {
                        @Override
                        public void onSuccess(Result<CreateOrderBody> result) {
                            if (result.isSuccess()) {
                                alertCallback.doConfirm(result.getData());
                                dialog.dismiss();
                            }
                        }

                        @Override
                        public void netLogin() {

                        }

                        @Override
                        public void onError(String errorStr) {
                            super.onError(errorStr);
                            Log.w("lxl", errorStr);

                        }

                    });

                }
            });
            dialog.setCancelable(getCancelable());


            dialog.setContentView(layout);
            dialog.setCanceledOnTouchOutside(getCanceledOnTouchOutside());
            Window dialogWindow = dialog.getWindow();
            dialogWindow.setGravity(getGravity());
            WindowManager m = dialogWindow.getWindowManager();
            Display d = m.getDefaultDisplay();
            WindowManager.LayoutParams p = dialogWindow.getAttributes();
            p.width = d.getWidth(); //设置dialog的宽度为当前手机屏幕的宽度
            dialogWindow.setAttributes(p);
            return dialog;
        }


        //实名认证
        public static CustomCommonDialog shiminDialog() {
            LayoutInflater inflater = (LayoutInflater) context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            final CustomCommonDialog dialog = new CustomCommonDialog(context, R.style.dialog);
            View layout = inflater.inflate(R.layout.popwindow_shiming, null);
            TextView tvCommit = layout.findViewById(R.id.tvCommit);
            TextView tvCancel = layout.findViewById(R.id.tvCancel);
            tvCancel.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    dialog.dismiss();
                }
            });
            tvCommit.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    context.startActivity(new Intent(context, ShiMinRenZhenActivity.class));

                }
            });
            dialog.setCancelable(getCancelable());


            dialog.setContentView(layout);
            dialog.setCanceledOnTouchOutside(getCanceledOnTouchOutside());
            Window dialogWindow = dialog.getWindow();
            dialogWindow.setGravity(getGravity());
            WindowManager m = dialogWindow.getWindowManager();
            Display d = m.getDefaultDisplay();
            WindowManager.LayoutParams p = dialogWindow.getAttributes();
            p.width = d.getWidth(); //设置dialog的宽度为当前手机屏幕的宽度
            dialogWindow.setAttributes(p);
            return dialog;
        }
        //联系客服
        public static CustomCommonDialog serverDialog() {
            LayoutInflater inflater = (LayoutInflater) context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            final CustomCommonDialog dialog = new CustomCommonDialog(context, R.style.dialog);
            View layout = inflater.inflate(R.layout.popwindow_kefu, null);
            ImageView ivClose = layout.findViewById(R.id.ivClose);
            ivClose.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    dialog.dismiss();
                }
            });
            dialog.setCancelable(getCancelable());
            dialog.setContentView(layout);
            dialog.setCanceledOnTouchOutside(getCanceledOnTouchOutside());
            Window dialogWindow = dialog.getWindow();
            dialogWindow.setGravity(getGravity());
            WindowManager m = dialogWindow.getWindowManager();
            Display d = m.getDefaultDisplay();
            WindowManager.LayoutParams p = dialogWindow.getAttributes();
            p.width = d.getWidth(); //设置dialog的宽度为当前手机屏幕的宽度
            dialogWindow.setAttributes(p);
            return dialog;
        }


        /**
         * 提示带button
         **/
        public static CustomCommonDialog hintwithButtonDialog(String hint) {
            LayoutInflater inflater = (LayoutInflater) context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            final CustomCommonDialog dialog = new CustomCommonDialog(context, R.style.dialog);
            View layout = inflater.inflate(R.layout.popwindow_hintwithbutton, null);
            TextView tvTip = layout.findViewById(R.id.tvTip);
            TextView tvButton = layout.findViewById(R.id.tvButton);
            tvTip.setText(hint);
            tvButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    dialog.dismiss();
                }
            });

            dialog.setCancelable(getCancelable());


            dialog.setContentView(layout);
            dialog.setCanceledOnTouchOutside(getCanceledOnTouchOutside());
            Window dialogWindow = dialog.getWindow();
            dialogWindow.setGravity(getGravity());
            return dialog;
        }

        /**
         * 规则明细
         **/
        public static CustomCommonDialog serverRuleDialog(String hint) {
            LayoutInflater inflater = (LayoutInflater) context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            final CustomCommonDialog dialog = new CustomCommonDialog(context, R.style.dialog);
            View layout = inflater.inflate(R.layout.popwindow_server_rule, null);
            TextView tvTip = layout.findViewById(R.id.tvTip);
            ImageView ivClose = layout.findViewById(R.id.ivClose);
            ImageView ivConfirm = layout.findViewById(R.id.ivConfirm);
            tvTip.setText(hint);
            ivClose.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    dialog.dismiss();
                }
            });
            ivConfirm.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    dialog.dismiss();
                }
            });

            dialog.setCancelable(getCancelable());


            dialog.setContentView(layout);
            dialog.setCanceledOnTouchOutside(getCanceledOnTouchOutside());
            Window dialogWindow = dialog.getWindow();
            dialogWindow.setGravity(getGravity());
            return dialog;
        }

        /**
         * 通用两按钮弹窗样式1
         **/
        public static CustomCommonDialog CommonTwoButtonDialog(AlertCallback alertCallback, String... strings) {
            LayoutInflater inflater = (LayoutInflater) context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            final CustomCommonDialog dialog = new CustomCommonDialog(context, R.style.dialog);
            View layout = inflater.inflate(R.layout.popwindow_commontwobutton, null);
            TextView tvTip = layout.findViewById(R.id.tvTip);
            TextView tvLeft = layout.findViewById(R.id.tvCancel);
            TextView tvRight = layout.findViewById(R.id.tvComfirm);
            if (strings != null) {
                if (strings.length > 0)
                    tvTip.setText(strings[0]);
                if (strings.length > 1)
                    tvLeft.setText(strings[1]);
                if (strings.length > 2)
                    tvRight.setText(strings[2]);
            }
            tvLeft.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    dialog.dismiss();
                    alertCallback.doConfirm(0);
                }
            });
            tvRight.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    dialog.dismiss();
                    alertCallback.doConfirm(1);
                }
            });

            dialog.setCancelable(getCancelable());


            dialog.setContentView(layout);
            dialog.setCanceledOnTouchOutside(getCanceledOnTouchOutside());
            Window dialogWindow = dialog.getWindow();
            dialogWindow.setGravity(getGravity());
            return dialog;
        }

        /**
         * 通用两按钮弹窗样式2
         **/
        public static CustomCommonDialog CommonTwoButtonDialogStyle2(AlertCallback alertCallback, String contexts) {
            LayoutInflater inflater = (LayoutInflater) context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            final CustomCommonDialog dialog = new CustomCommonDialog(context, R.style.dialog);
            View layout = inflater.inflate(R.layout.popwindow_commontwobuttonstyle2, null);
            TextView tvLeft = layout.findViewById(R.id.tvCancel);
            TextView tvRight = layout.findViewById(R.id.tvComfirm);
            TextView tvTip = layout.findViewById(R.id.tvTip);
            tvTip.setText(contexts);

            tvLeft.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    dialog.dismiss();
                    alertCallback.doConfirm(0);
                }
            });
            tvRight.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    dialog.dismiss();
                    alertCallback.doConfirm(1);
                }
            });

            dialog.setCancelable(getCancelable());


            dialog.setContentView(layout);
            dialog.setCanceledOnTouchOutside(getCanceledOnTouchOutside());
            Window dialogWindow = dialog.getWindow();
            dialogWindow.setGravity(getGravity());
            return dialog;
        }

        public static CustomCommonDialog notifyDialog(AlertCallback alertCallback) {
            LayoutInflater inflater = (LayoutInflater) context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            final CustomCommonDialog dialog = new CustomCommonDialog(context, R.style.dialog);
            View layout = inflater.inflate(R.layout.popwindow_notify, null);
            TextView tvCancel = layout.findViewById(R.id.tvCancel);
            TextView tvComfirm = layout.findViewById(R.id.tvComfirm);
            EditText etContent = layout.findViewById(R.id.etContent);

            tvCancel.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    dialog.dismiss();
                }
            });
            tvComfirm.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (!TextUtils.isEmpty(etContent.getText().toString())) {
                        dialog.dismiss();
                        alertCallback.doConfirm(etContent.getText().toString());
                    }
                }
            });

            dialog.setCancelable(getCancelable());


            dialog.setContentView(layout);
            dialog.setCanceledOnTouchOutside(getCanceledOnTouchOutside());
            Window dialogWindow = dialog.getWindow();
            dialogWindow.setGravity(getGravity());


            WindowManager m = dialogWindow.getWindowManager();
            Display d = m.getDefaultDisplay();
            WindowManager.LayoutParams p = dialogWindow.getAttributes();
            p.width = d.getWidth() - 100; //设置dialog的宽度为当前手机屏幕的宽度
            dialogWindow.setAttributes(p);


            return dialog;
        }
    }
}
